from aip import AipFace  #pip install baidu-aip
import base64

#人脸识别测试类
class  BaiDuFace:
    def __init__(self,APP_ID="118341141",API_KEY="dbr2vrVaFwVaguejXfO028CI",SECRET_KEY="uI048shdD24ePisjeepEtjJ9691jtaDA"):
        self.APP_ID = APP_ID
        self.API_KEY = API_KEY
        self.SECRET_KEY = SECRET_KEY

        self.client = AipFace(self.APP_ID, self.API_KEY, self.SECRET_KEY)

        #人脸注册
    def add_user(self):
            #要注册的人脸照片
            data = base64.b64encode(open('./test.jpg', 'rb').read()).decode('utf-8')
            image = data

            imageType = "BASE64"

            groupId = "test_zdd_20250403"  #组ID

            userId = "zhaodi"

            """ 调用人脸注册 """
            # self.client.addUser(image, imageType, groupId, userId);

            """ 如果有可选参数 """
            options = {}
            options["user_info"] = "这是用户介绍"
            options["quality_control"] = "NORMAL"
            options["liveness_control"] = "LOW"
            options["action_type"] = "REPLACE"

            """ 带参数调用人脸注册 """
            res = self.client.addUser(image, imageType, groupId, userId, options)
            return res

        #删除人脸
    def delete(self):
            userId = "zhaodi"
            groupId = "test_zdd_20250403"
            faceToken = "be6b70065b093f2e7c08810f5daf58c6" #这个是注册完人脸才会返回的
            """ 调用人脸删除 """
            res = self.client.faceDelete(userId, groupId, faceToken);
            return res

        #搜索人脸
    def search(self):
            data = base64.b64encode(open('./test.jpg', 'rb').read()).decode('utf-8')
            image = data
            imageType = "BASE64"
            groupIdList = "test_zdd_20250403"  #多个组用逗号隔开

            """ 如果有可选参数 """
            # options = {}
            # options["match_threshold"] = 70
            # options["quality_control"] = "NORMAL"
            # options["liveness_control"] = "LOW"
            # # options["user_id"] = "233451"
            # options["max_user_num"] = 3

            """ 带参数调用人脸搜索 """
            # res = self.client.search(image, imageType, groupIdList, options)
            """ 调用人脸搜索 """
            res = self.client.search(image, imageType, groupIdList);
            print(res)

if __name__ == '__main__':
    ai = BaiDuFace()

    # res = ai.add_user()  #注册人脸 be6b70065b093f2e7c08810f5daf58c6
    # res = ai.delete() # 删除人脸
    res = ai.search()  #搜索人脸
    print(res)